package com.owen.oauth2.jdbc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private JdbcClientDetailsService clientRegistrationService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/client")
    public String client(String clientId,String password,String scope){
        BaseClientDetails clientDetails=new BaseClientDetails();
        clientDetails.setClientId(clientId);
        clientDetails.setClientSecret(passwordEncoder.encode(password));
        clientDetails.setScope(Arrays.asList(scope));
        clientRegistrationService.addClientDetails(clientDetails);
        return "success";
    }

}
